<template>
	<view>
		<uni-nav-bar :fixed="true" left-icon="back" color="#156DAC" @clickLeft="pageback">
			<view class="header-icon-text-panel">
				<text class="title">客户查询</text>
				<view style="margin-left: 10upx;" @click="showDrawer">
					<uni-icons type="search" size="16" color="#156DAC"></uni-icons>
				</view>
			</view>
		</uni-nav-bar>
		<view class="content padding">
			<view class="page-content-list">
				<block v-for="(value, index) in listData" :key="index">
					<view class="content-list-cell"  @click="goDetail(value)">
						<view class="order-item">
							<view class="order-item-content">
								<view class="order-content-row">
									<view class="label">企业名称</view><view class="value" style="color: #299AEB;">{{value.name}}</view>
								</view>
								<view class="order-content-row">
									<view class="label">法人代表</view><view class="value" style="color: #299AEB;">{{value.legalRepresentative || '- -'}}</view>
								</view>
								<view class="order-content-row">
									<view class="label">控制级别</view><view class="value">{{value.controlLevelVal || '- -'}}</view>
								</view>
								<view class="order-content-row">
									<view class="label">行业类别</view><view class="value">{{value.industryCategoryVal || '- -'}}</view>
								</view>
								<view class="order-content-row">
									<view class="label">企业地址</view><view class="value">{{value.enterpriseAddressDetails || '- -'}}</view>
								</view>
								<view class="order-content-row">
									<view class="label">联系方式</view><view class="value">{{value.enterpriseName || '- -'}}</view>
								</view>
								<!-- <view class="order-content-row">
									<view class="label">操作</view>
									<view class="value">
										<button class="order-btn">评价</button>
									</view>
								</view> -->
							</view>
						</view>
					</view>
				</block>
			</view>
			<uni-load-more :status="status" :icon-size="16" :content-text="contentText" />
		</view>
		
		<uniDrawer ref="draw" :mask="true" mode='right' :width="250">
			<view style="height: 90upx;">&nbsp;</view>
			<view class="draw-panel">
				<view class="drawer-form">
					<view class="drawer-form-item">
						<view class="drawer-label"><text>区域</text></view>
						<view class="drawer-content">
							<text @click="showPkPicker">{{query.addressName || '请选择区域'}}</text>
							<tki-tree class="ipt-ctl" ref="tkitree" :selectParent="true" :range="range" :rangeKey="rangeKey" idKey="areacode" rangeKey="areaname" confirmColor="#4e8af7" @confirm="pkConfirm"/>
						</view>
					</view>
					<view class="drawer-form-item">
						<view class="drawer-label"><text>关键字</text></view>
						<view class="drawer-content">
							<uni-easyinput class="ipt-ctl" type="text" v-model="query.queryField" maxlength="8" placeholder="请输入关键字" />
						</view>
					</view>
				</view>
				<view class="operation-panel">
					<view class="item-card">
						<view class="card-title">
							<uni-icons class="icon left" type="gear" size="20" color="#156dac"></uni-icons>
							<text class="content">控制级别</text>
							<uni-icons class="icon right" type="arrowdown" size="16" color="#666"></uni-icons>
						</view>
						<view class="operation-panel flex" style="padding: 20upx 0 0  0;">
							<view 
								v-for="(item,index) in controlLevels" 
								class="item-btn mini auto" 
								:class="query.controlLevel === (item.code)?'':'white'" 
								@click="pickerConfim('controlLevel',item)" 
								:key="index"
							>
								<text>{{item.codeName}}</text>
							</view>
						</view>
					</view>
					<view class="item-card">
						<view class="card-title">
							<uni-icons class="icon left" type="flag" size="20" color="#156dac"></uni-icons>
							<text class="content">行业类别</text>
							<uni-icons class="icon right" type="arrowdown" size="16" color="#666"></uni-icons>
						</view>
						<view class="operation-panel flex" style="padding: 20upx 0 0  0;">
							<view 
								v-for="(item,index) in industryCategorys" 
								class="item-btn mini auto" 
								:class="query.industryCategory === (item.code)?'':'white'" 
								@click="pickerConfim('industryCategory',item)" 
								:key="index"
							>
								<text>{{item.codeName}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="bottom-panel">
					<!-- <view class="item-btn green lp"><text>重置</text></view> -->
					<view class="item-btn lp" @click="handleQuery"><text>确定</text></view>
				</view>
			</view>
		</uniDrawer>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import uniIcons from "@/components/uni-icons/uni-icons.vue"
	import uniDrawer from "@/components/uni-drawer/uni-drawer.vue"
	import tkiTree from "@/components/tki-tree/tki-tree.vue"
	import LbPicker from '@/components/lb-picker'
	import {getDict} from '@/common/dict.js'
	
	const getTree = '/base/area/tree'
	const listQuery = '/base/enterprise/listPage'
	export default {
		components:{
			uniIcons,
			uniLoadMore,
			uniDrawer,
			LbPicker,
			tkiTree
		},
		data() {
			return {
				reload: false,
				listData: [],
				last_id: '',
				status: 'loading',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				pickerUserProps: {
					label: 'realName',
					value: 'id',
				},
				range: [],
				rangeKey: 'name',
				query: {
					total: 0,
					page: 1,
					limit: 10,
					controlLevel: '',
					industryCategory: '',
					queryField: '',
					addressPId: '',
					enterpriseCityId: '',
					enterpriseAddressId: '',
					addressName: '',
				},
				dept: '',
				companyName: '',
				// 字典
				controlLevels: [],
				industryCategorys: [],
				chooseUserList: [],
				
			}
		},
		onLoad() {
			
			this.getTreeData();
			this.getList();
			// 字典
			this.controlLevels = [{code:'',codeName:'全部'},...getDict('e014')]
			this.industryCategorys = [{code:'',codeName:'全部'},...getDict('e013')]
		},
		onPullDownRefresh() {
			this.last_id = '';
			this.listData =  []
			this.getList();
			setTimeout(()=>{
				uni.stopPullDownRefresh();
			},100)
		},
		onReachBottom() {
			this.status = 'more';
			this.getList();
		},
		methods: {
			handleQuery() {
				this.query.page = 1
				this.getList();
				this.$refs.draw.close()
			},
			pageback(){
				uni.navigateBack({
					delta: 1
				});
			},
			getTreeData() {
				this.$api.request(getTree,{
					method: 'get',
				}).then(res=>{
					if(res.success){
						// const result = []
						// res.data.list.map(item=>{
						// 	if(item.children && item.children.length > 0){
						// 		result.push(item)
						// 	}
						// })
						this.range = res.data.list
					}
				}).catch(()=>{
				})
			},
			showPkPicker() {
				this.$refs.tkitree._show()
			},
			pkConfirm(e) {
				if(e.length > 0){
					const {areaid,areaname} = e[0]
					console.log(e)
					this.query.addressPId = areaid
					this.query.addressName = areaname
				}
			},
			showDrawer(){
				this.$refs.draw.open()
			},
			goDetail(item){
				const {id} = item
				uni.navigateTo({
					url:`/pages/adminPages/customer/customerDetail/customerDetail?id=${id}`
				})
			},
			pickerSelectConfim(key,e){
				const {item} = e
				const {id, realName} = item
				this.query[key+ 'Id'] = id
				this.query[key+'Name'] = realName
			},
			pickerConfim(key, item){
				const {code, codeName} = item
				this.query[key] = code
				this.query[key+'Name'] = codeName
			},
			handleAdd(){
				
			},
			getList(loadMore = false){
				this.status = 'loading';
				this.$api.request(listQuery,{
					method: 'get',
					data: this.query
				}).then(res=>{
				console.log(res)
					if(res.success) {
						const {data} = res.data
						const {list} = data
						if(list.length === 0 || data.total <= this.query.limit){
							this.status = ''
						}
						if(loadMore){
							this.listData = [...this.listData,...list]
						}else{
							this.listData  = list
						}
						this.last_id = this.listData[this.listData.length - 1];
						this.reload = false;
					}else{
						this.$api.toastMsg(res.message)
					}
				}).catch((e)=>{
					console.log(e)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	
	.page-content-list{
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
		.content-list-cell{
			position: relative;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
		}
	}
	.order-item{
		width: 750upx;
		background: #fff;
		padding: 20upx;
		border-radius: 20upx;
		margin-bottom: 20upx;
		.order-item-content{
			margin-top: 30upx;
			.order-content-row{
				display: flex;
				align-items: flex-start;
				justify-content: space-between;
				font-size: 26upx;
				margin-bottom: 30upx;
				.order-btn{
					display: inline-block;
					background-color: #156dac;
					color: #fff;
					width: 95upx;
					height: 50upx;
					line-height: 50upx;
					text-align: center;
					font-size: 24upx;
					padding: 0;
					margin-left: 10upx;
				}
				.label{
					flex: 0 0 200upx;
					color: #5A5A5A;
					text-align: left;
					font-size: 26upx;
				}
				.value{
					flex: 1;
					color: #000;
					text-align: right;
					font-size: 26upx;
					&.text-left{
						text-align: left;
						padding-left: 20upx;
					}
				}
			}
			
		}
			
		.order-item-header{
			display: flex;
			align-items: center;
			.icon{
				margin-right: 10upx;
			}
			text{
				color: #333;
				font-size: 26upx;
				&.green{
					color:#2AD51D;
				}
			}
			
		}
	}
</style>
